package edu.luas.digitalmedia.dao;

import edu.luas.digitalmedia.pojo.MyAddress;

import java.sql.Connection;
import java.sql.PreparedStatement;

public class MyAddressDao {
    /*
     * @description: 添加收货地址
     * @author: sjw
     * @date:
     * @param: 添加的地址实体对象
     * @return: 添加影响的行数
     **/
    public int insert(MyAddress model) throws Exception {
        Connection connection=null;
        PreparedStatement statement = null;
        try {
            //1:从连接池中获取 connection 对象
            connection = DBHelper.getConnection();

            //2:创建执行语句对象 statement
            String sql = "insert into myaddress (`name`,  address, tel) values(?,?,?) ";
            statement = connection.prepareStatement(sql);
            statement.setObject(1, model.getName());
            statement.setObject(2, model.getAddress());
            statement.setObject(3, model.getTel());

            //3：执行 sql 语句，返回影响行数
            int count = statement.executeUpdate();

            //4:处理结果
            return count;
        }catch (Exception ex){
            ex.printStackTrace();
            throw ex;
        }finally {
            //5：关闭资源
            DBHelper.close(statement,connection);
        }
    }
}
